package com.chenjl.serialization.support;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.chenjl.Constant;
import com.chenjl.serialization.Serialization;
/**
 * Hessian序列化，序列化BigDecimal有问题
 * 2016-9-2 11:27:06
 * @author chenjinlong
 */
public class HessianSerialization implements Serialization {
	@Override
	public String getContentType() {
		return Constant.HESSIAN_CONTENT_TYPE;
	}
	@Override
	public byte[] toByte(Object object) throws IOException {
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		Hessian2Output hessian2Output = new Hessian2Output(byteArrayOutputStream);
		hessian2Output.writeObject(object);
		hessian2Output.flushBuffer();
		
		byte[] bytes = byteArrayOutputStream.toByteArray();
		byteArrayOutputStream.close();
		hessian2Output.close();
		return bytes;
	}
	@Override
	public Object toObject(byte[] bytes) throws IOException {
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
		Hessian2Input hessian2Input = new Hessian2Input(byteArrayInputStream);
		Object object = hessian2Input.readObject();
		
		byteArrayInputStream.close();
		hessian2Input.close();
		return object;
	}
}